﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using SpLocalizer.Common;
using Microsoft.SharePoint;
using System.Xml;
using Microsoft.Practices.Unity;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using DocumentFormat.OpenXml.Wordprocessing;


namespace SpLocalizer.ContentSourceProviders.SpDocumentLibraryFile
{
    internal class WordFileLocalizationTask : AbstractFileLocalizationTask
    {
         /// <summary>
        /// Constructor without parameters should be public for unity injection
        /// </summary>
        public WordFileLocalizationTask() { }
    
        protected override string GetDocumentXmlToTranslate(SPListItem item)
        {            
                // Open a WordprocessingDocument for read-only access based on a stream.
                using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(item.File.OpenBinaryStream(), false))
                {
                    return wordDocument.MainDocumentPart.Document.OuterXml;
                }
        }

        internal override void ExportTranslation(SPListItem item)
        {
            if (!Constants.FILE_WORD.Equals(this.Format)){
                throw new ApplicationException(String.Format("Illegal format {0} for export with {1}. {2} Required",
                    this.Format, typeof(WordFileLocalizationTask).Name, Constants.FILE_WORD));
            }
            
                SPFile theFile = item.File;
                // Open a WordprocessingDocument for read-only access based on a stream.
                Stream documentStream = theFile.OpenBinaryStream();
                using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(documentStream, true))
                {

                    XmlDocument XmlDoc = new XmlDocument();
                    XmlDoc.LoadXml(this.TranslatedText);

                    wordDocument.MainDocumentPart.Document.InnerXml = XmlDoc.DocumentElement.InnerXml;
                    wordDocument.Close();
                    SafeFile(theFile, documentStream);

                    /* Validate document
                    OpenXmlValidator validator = new OpenXmlValidator();
                    List<ValidationErrorInfo> errors = validator.Validate(wordDocument).ToList<ValidationErrorInfo>();
                    StringBuilder errorText = new StringBuilder();
                    if (errors.Count > 0)
                    {
                        int count = 0;
                        foreach(ValidationErrorInfo error in errors){
                            errorText.AppendFormat("\nError {0}",count);
                            errorText.AppendFormat("\nDescription: {0}", error.Description);
                            errorText.AppendFormat("\nErrorType: {0}", error.ErrorType);
                            errorText.AppendFormat("\nNode: {0}", error.Node);
                            errorText.AppendFormat("\nPath: {0}", error.Path.XPath);
                            errorText.AppendFormat("\nPart: {0}", error.Part.Uri);
                            errorText.AppendFormat("\n-------------------------------------------\n");                        
                        }
                    
                        
                        throw new ApplicationException( String.Format("Error exporting translation for file {0}. Exceptions: \n{1}\n", item.Url, errorText.ToString()));
                    }*/

                   

                }
            }        
    }
}
